<?php

class FueditorController extends Controller{
    public $layout='column2';
    public function actionIndex(){
        /* 上传图片配置项 */
        $config=array(
            "imageActionName"=> "uploadimage",
            "imageFieldName"=> "upfile",
            "imageMaxSize"=> 2048000,
            "imageAllowFiles"=> array(".png", ".jpg", ".jpeg", ".gif", ".bmp"),
            "imageCompressEnable"=> true, 
            "imageCompressBorder"=> 1600,
            "imageInsertAlign"=> "none",
            "imageInsertAlign"=> "none",
            "imageUrlPrefix"=> "", 
            "imagePathFormat"=> "/files/ueditor/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}", );
        $action = $_GET['action'];
        switch ($action) {
            case 'config':
                $result =  json_encode($config);
                break;

            /* 上传图片 */
            case 'uploadimage':
            /* 上传涂鸦 */
            case 'uploadscrawl':
            /* 上传视频 */
            case 'uploadvideo':
            /* 上传文件 */
            case 'uploadfile':
                $result = self::upload($action,$config);
                break;

            /* 列出图片 */
            case 'listimage':
                $result = include("action_list.php");
                break;
            /* 列出文件 */
            case 'listfile':
                $result = include("action_list.php");
                break;

            /* 抓取远程文件 */
            case 'catchimage':
                $result = include("action_crawler.php");
                break;

            default:
                $result = json_encode(array(
                    'state'=> '请求地址出错'
                ));
                break;
        }
        if (isset($_GET["callback"])) {
            if (preg_match("/^[\w_]+$/", $_GET["callback"])) {
                echo htmlspecialchars($_GET["callback"]) . '(' . $result . ')';
            } else {
                echo json_encode(array(
                    'state'=> 'callback参数不合法'
                ));
            }
        } else {
            echo $result;
        }
    }
    
    function Upload($type,$CONFIG){
        Yii::import("ext.ueditor.php.Uploader");
        $base64 = "upload";
        switch (htmlspecialchars($type)) {
            case 'uploadimage':
                $config = array(
                    "pathFormat" => $CONFIG['imagePathFormat'],
                    "maxSize" => $CONFIG['imageMaxSize'],
                    "allowFiles" => $CONFIG['imageAllowFiles']
                );
                $fieldName = $CONFIG['imageFieldName'];
                break;
            case 'uploadscrawl':
                $config = array(
                    "pathFormat" => $CONFIG['scrawlPathFormat'],
                    "maxSize" => $CONFIG['scrawlMaxSize'],
                    "allowFiles" => $CONFIG['scrawlAllowFiles'],
                    "oriName" => "scrawl.png"
                );
                $fieldName = $CONFIG['scrawlFieldName'];
                $base64 = "base64";
                break;
            case 'uploadvideo':
                $config = array(
                    "pathFormat" => $CONFIG['videoPathFormat'],
                    "maxSize" => $CONFIG['videoMaxSize'],
                    "allowFiles" => $CONFIG['videoAllowFiles']
                );
                $fieldName = $CONFIG['videoFieldName'];
                break;
            case 'uploadfile':
            default:
                $config = array(
                    "pathFormat" => $CONFIG['filePathFormat'],
                    "maxSize" => $CONFIG['fileMaxSize'],
                    "allowFiles" => $CONFIG['fileAllowFiles']
                );
                $fieldName = $CONFIG['fileFieldName'];
                break;
        }
        $up = new Uploader($fieldName, $config, $base64);
        return json_encode($up->getFileInfo());
    }
    
    public function actionSendEmail(){
        Yii::import('ext.mail.PHPMailer');
        $mail = new PHPMailer(); //建立邮件发送类
        $address = "605338730@qq.com";
        $mail->IsSMTP(); // 使用SMTP方式发送
        $mail->CharSet = 'UTF-8'; 
        $mail->Host = "smtp.163.com"; 
        $mail->Port="25";
        $mail->SMTPAuth = true; 
        $mail->Username = "redmine_manager@163.com"; // 163电邮用户名
        $mail->Password = "linghang"; // 密码
        $mail->From = "redmine_manager@163.com"; //邮件发送者email地址
        $mail->FromName = "朱晓辉";
        $mail->AddAddress("$address", ""); //收件人地址，可以替换成任何想要接收邮件的email信箱,格式是AddAddress("收件人email","收件人姓名")
        
        //$mail->AddReplyTo("", "");
        //$mail->AddAttachment("/var/tmp/file.tar.gz"); // 添加附件
        $mail->IsHTML(true); // set email format to HTML //是否使用HTML格式              
        $mail->Subject = "专业建设与职业发展管理平台密码修改"; //邮件标题
        $mail->Body = "123"; //邮件内容
        $mail->AltBody = "This is the body in plain text for non-HTML mail clients"; //附加信息，可以省略
        if (!$mail->Send()) {
           return false;
            exit;
        } else {
            return TRUE;
        }  
    }
}
